﻿// -----------------------------------------------------------------------
// <copyright file="NodeWFRoleDefaultData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db NodeWFRoleDefaultData
    /// </summary>
    public class NodeWFRoleDefaultData
    {
        /// <summary>
        /// Get NodeWFRoleDefault By Id
        /// </summary>
        /// <param name="nodeWFRoleDefaultId">Param nodeWFRoleDefaultId</param>
        /// <returns>Return Position</returns>
        public NodeWFRoleDefaultEN GetNodeWFRoleDefaultById(int nodeWFRoleDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                NodeWFRoleDefaultEN nodeWFRoleDefault = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeFWRoleDefaultGetById, nodeWFRoleDefaultId);
                while (reader.Read())
                {
                    nodeWFRoleDefault = new NodeWFRoleDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWFRoleDefault.NodeWFRoleDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWFRoleDefault.NodeWorkflowRollId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWFRoleDefault.ProfileTypeId = reader.GetInt32(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return nodeWFRoleDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
        
        /// <summary>
        /// Insert to db nodeWfRoleDefault
        /// </summary>
        /// <param name="position">Param nodeWfRoleDefault</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(NodeWFRoleDefaultEN nodeWfRoleDefault)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.NodeFWRoleDefault.NodeWorkflowRollId, nodeWfRoleDefault.NodeWorkflowRollId),
                                    new SqlParameter(Constants.SqlParamerterName.NodeFWRoleDefault.ProfileTypeId, nodeWfRoleDefault.ProfileTypeId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeFWRoleDefaultInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Position by nodeWFRoleDefaultId
        /// </summary>
        /// <param name="nodeWFRoleDefaultId">param nodeWFRoleDefaultId</param>
        public void Delete(int nodeWFRoleDefaultId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.NodeFWRoleDefault.NodeWFRoleDefaultId, nodeWFRoleDefaultId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeFWRoleDefaultDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list NodeWFRoleDefaultEN
        /// </summary>
        /// <returns>Return list all NodeWFRoleDefaultEN</returns>
        public IList<NodeWFRoleDefaultEN> GetFWRoleDefaultByNodeWorkflowRollId(int nodeWorkflowRollId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<NodeWFRoleDefaultEN> lstNodeFWRoleDefault = new List<NodeWFRoleDefaultEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneNodeWFRoleDefaultGetByNodeWorkflowRollId, nodeWorkflowRollId);
                while (reader.Read())
                {
                    NodeWFRoleDefaultEN nodeWfRoleDefault = new NodeWFRoleDefaultEN();

                    if (!reader.IsDBNull(0))
                    {
                        nodeWfRoleDefault.NodeWFRoleDefaultId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        nodeWfRoleDefault.NodeWorkflowRollId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        nodeWfRoleDefault.ProfileTypeId = reader.GetInt32(2);
                    }

                    lstNodeFWRoleDefault.Add(nodeWfRoleDefault);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstNodeFWRoleDefault;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}